package com.atguigu.base;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

/**
 * @PACKAGE_NAME: com.atguigu.base
 * @CLASSNAME: BaseController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/16 19:56
 * @SINCE 17.0.7
 * @DESCRIPTION: BaseController
 */
public class BaseController {

    /**
     * 封装页面提交的分页参数以及搜索条件
     * @param request
     * @return
     */
    protected Map<String, Object> getFilters(HttpServletRequest request) {
        //1.拿到前端传递的所有的请求参数,及其对应的值
        Enumeration paramNames = request.getParameterNames();
        Map<String, Object> filters = new TreeMap<>();
        while (paramNames != null && paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            String[] values = request.getParameterValues(paramName);
            if (null != values && values.length != 0) {
                if (values.length > 0) {
                    filters.put(paramName, values[0]);
                } else {
                    filters.put(paramName, values[0]);
                }
            }
        }
        //2.判断这些请求参数中包不包含pageNum和pageSize
        if (!filters.containsKey("pageNum")) {
            filters.put("pageNum", 1);
        }
        if (!filters.containsKey("pageSize")) {
            filters.put("pageSize", 5);
        }
        return filters;
    }
}
